package oop.ex1.filters;

import java.io.File;
import java.util.TreeSet;

//###############
//FILE : DiskAttributesFilter.java
//WRITER : Amir Wilf, wilfoa, 300007085
//WRITER : Daniel Acherkan, danithelion, 310116348
//EXERCISE : oop ex1 2011
//DESCRIPTION:
//A file filter which filter files by their disk attributes
//###############

public class DiskAttributesFilter extends Filter {
	
	//Constants:
	private final char READABLE = 'r';
	private final char WRITABLE = 'w';
	private final char EXECUTABLE = 'x';
	
	
    //Data Members:
	//_attribute - the attribute in question - readable/writble/executable
  	protected char _attribute;
  	
  	//_degree - filter's "direction" - Y/N  
	protected boolean _degree;
	

    //METHODS
	/**
	 * Constructor for the class.
	 * @param attribute the attribute in question - readable/writble/executable 
	 * @param degree 'true' for yes and 'false' for no
	 */
	public DiskAttributesFilter(char attribute,boolean not, boolean degree){
		super(not);
		_attribute = attribute;
	    _degree = degree;
	}
	
	/**
	 * the method filters the files according to the given parameters
	 * @param directoryPath The directory to filter in
	 * @return A TreeSet list of the files which matched the filter
	 */
	@Override
	public TreeSet<File> filter(TreeSet<File >fullDirectoryFiles) {
		TreeSet<File> list = new TreeSet<File>();
		for(File file : fullDirectoryFiles){
			//call a helper method the match the right 'degree'
			if(canDo(file)){
				list.add(file);
			}
		}
		return addNegation(list,fullDirectoryFiles);
	}
	
	//A helper method which matches the right case according to the attribute
	private boolean canDo(File file){
		switch(_attribute){
			case READABLE:{
				if(_degree){
					return file.canRead();
				}
				else{
					return !file.canRead();
				}
			}
			case WRITABLE:{
				if(_degree){
					return file.canWrite();
				}
				else{
					return !file.canWrite();
				}
			}
			case EXECUTABLE:{
				if(_degree){
					return file.canExecute();
				}
				else{
					return !file.canExecute();
				}
			}
		}
		return false;
	}
}
